package com.dk.service.impl;

import com.dk.mapper.SslRoleMapper;
import com.dk.mapper.SslRoleMenuMapper;
import com.dk.pojo.SslRole;
import com.dk.pojo.SslRoleExample;
import com.dk.pojo.SslRoleMenu;
import com.dk.pojo.SslRoleMenuExample;
import com.dk.service.SslRoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.Ssl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Service
public class SslRoleServiceImpl implements SslRoleService {
    @Autowired
    private SslRoleMapper roleMapper;
    @Autowired
    private SslRoleMenuMapper roleMenuMapper;

    @Override
    public List<SslRole> findRoleList() {

        return roleMapper.selectByExample(new SslRoleExample());
    }

    @Override
    public void saveRole(SslRole trole, Integer[] nodesIds) {
        // 1、保存角色数据，并返回角色id
        int i = roleMapper.insertRoleAndId(trole);
        System.out.println(i);
        // 2、维护角色权限中间表
        // 获取角色id
        Integer roleId = trole.getRoleId();
        // 定义一个集合，用来装载角色权限中间表对象
        List<SslRoleMenu> roleMenus = new ArrayList<>();
        // 遍历权限数组
        for (Integer menuId : nodesIds) {
            SslRoleMenu roleMenu = new SslRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            roleMenus.add(roleMenu);
        }
        int j = roleMenuMapper.addBatch(roleMenus);
        System.out.println(j);
    }

    @Override
    public SslRole findRoleById(Integer roleId) {

        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public void updateRole(SslRole trole, Integer[] nodesIds) {
        // 1、修改角色
        roleMapper.updateByPrimaryKey(trole);
        // 2、删除中间表数据
        SslRoleMenuExample example = new SslRoleMenuExample();
        SslRoleMenuExample.Criteria criteria = example.createCriteria();
        // 将角色对象中的id取出，放入criteria条件中
        criteria.andRoleIdEqualTo(trole.getRoleId());
        roleMenuMapper.deleteByExample(example);
        // 3、重新维护第三张表
        // 获取角色id
        Integer roleId = trole.getRoleId();
        // 定义一个集合，用来装载角色权限中间表对象
        List<SslRoleMenu> roleMenus = new ArrayList<>();
        // 遍历权限数组
        for (Integer menuId : nodesIds) {
            SslRoleMenu roleMenu = new SslRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            roleMenus.add(roleMenu);
        }
        int j = roleMenuMapper.addBatch(roleMenus);
    }

    @Override
    public void deleteRole(Integer roleId) {
        // 1、删除角色数据
        roleMapper.deleteByPrimaryKey(roleId);
        // 2、删除角色权限中间表
        SslRoleMenuExample example = new SslRoleMenuExample();
        SslRoleMenuExample.Criteria criteria = example.createCriteria();
        // 将角色对象中的id取出，放入criteria条件中
        criteria.andRoleIdEqualTo(roleId);
        roleMenuMapper.deleteByExample(example);
    }

    @Override
    public List<Integer> findRoleListByPersonId(Integer pid) {

        return roleMapper.findRoleListByPersonId(pid);
    }
}
